<?php

namespace app\admin\controller;
class Ucase extends base
{
    //前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
        'delfile' => ['only' => 'del'],
    ];


    /**
     * 删除文件
     */
    protected function delfile()
    {
        $file = $this->model->where('id', $this->request->param('id'))->value('image');

        if (!empty($file) && file_exists('./uploads/image/' . $file)) {
            @unlink('./uploads/image/' . $file);
            @unlink('./uploads/image/sm_' . $file);
            @unlink('./uploads/image/md_' . $file);
            @unlink('./uploads/image/lg_' . $file);
        }

        $images = model('UcaseImages')->where('pid', $this->request->param('id'))->column('image');
        if ($images) {
            foreach ($images as $key) {
                @unlink('./uploads/image/sm_' . $key);
                @unlink('./uploads/image/lg_' . $key);
                @unlink('./uploads/image/md_' . $key);
                @unlink('./uploads/image/' . $key);
            }
        }
        model('UcaseImages')->destroy(['pid' => $this->request->param('id')]);
    }

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (!empty($data['cid'])) {
                $this->datas ['cid'] = $data['cid'];
            }
            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
    }


    /**
     * 编辑
     */
    public function edit($id)
    {
        $data = $this->model->find($id);

        if (!$data) {
            $this->error('数据不存在');
        }

        //更多图片
        $data['images'] = model('UcaseImages')->where('pid', $data['id'])->select();

        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 保存
     */
    public function save()
    {

        if (!$this->request->isPost()) {
            $this->error('请求失败');
        }
        $data = $this->request->post();

        $result = $this->validate($data, 'Ucase');
        if (true !== $result) {
            $this->error($result);
        }

        if (!empty($data['id'])) {
            if (!empty($data['images'])) {
                model('UcaseImages')->destroy(['pid' => $data['id']]);
                foreach ($data['images'] as $key => $value) {
                    if (!empty($value)) {
                        $image[] = ['image' => $value, 'pid' => $data['id']];
                    }
                }
                model('UcaseImages')->saveAll($image);
            }

            //删除产品图片
            if ($data['old_image'] != $data['image']) {
                @unlink('./uploads/image/' . $data['old_image']);
                @unlink('./uploads/image/sm_' . $data['old_image']);
                @unlink('./uploads/image/md_' . $data['old_image']);
                @unlink('./uploads/image/lg_' . $data['old_image']);
            }


            return $this->update($data);
        }

        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            if (!empty($data['images'])) {
                foreach ($data['images'] as $key => $value) {
                    if (!empty($value)) {
                        $image[] = ['image' => $value, 'pid' => $this->model->id];
                    }
                }
                model('UcaseImages')->saveAll($image);
            }
            return ajax_info('保存成功','y');
        } else {
            return ajax_info('添加失败');
        }
    }


    /**
     * 删除单个图片
     */
    public function delimage()
    {
        if ($this->request->isAjax()) {
            if ($image = $this->request->param('image')) {
                @unlink('./uploads/image/' . $image);
                @unlink('./uploads/image/sm_' . $image);
                @unlink('./uploads/image/md_' . $image);
                @unlink('./uploads/image/lg_' . $image);
                model('UcaseImages')->where('image',$image)->delete();
                return json(['msg' => '1','value'=>'成功删除图片！']);
            }
            return json(['msg' => '0','value'=>'删除图片失败！']);

        }
    }
}
